package com.atjinggu.jinggumail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atjinggu.jinggumail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117612032";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCvZafbjehDb9BRk6ugy7Sp6gzZ48Jvey9NAHisVlonIijcEKgRONbcIjxJYBfEh/HHXx2F4eEnozWzv3AzrpSk+bhjBkp6CzQ819A2sTmwGjEaayMqyRa4+HW16BkigMYDrqvlenSdlLUOFz/lnEhrHV2yvMDcO1QqWMsXFxNEtQ8kmBOhRRJ8ikhimDPLP+rHT1P+xxVv7smNINB7uDk27gjvhf70cnA/nk9AHXWs+z/m8WD0W0Tdkq707UgkmABX99FTUGW+p2dh453xw8QmO8PO8HX7tXuQ9PHCFwjcm6tiyFqE4vpQHNnV45zIUzVVuWNcpN0fnVuqGINNBEn5AgMBAAECggEAB6mKNrYxUljdIeMwmTqs7stRumcPxGz+OP/vRaAUGieM32g+31m7USyRs6bmAujpcAtAr+uZOXEQ1IRlc2yKA9cA7hqFQGWi55GKAAmn+oB6aqwJgkH8oyH/im3UQ0Sa4ehMjB7YX1XyvuXXHcHBDCThRgCm8DgLSjpMzPpuFg88TQxFsuRyEP0DMezZgzu9SU65unCkBU6PP+osdrZcQi8wWxCJ4ENrSv2kIk+Gxn04BCTdI4FB/zZcPSye9RJHihJkp2yh+8s4pI7xHySteWxLoCKnkMMRPZ5mY8gS2dESZduPXGbo6WPXsDd/WVAxd8ou7jZ0g73lBiGJw8LMHQKBgQDdSz/Yv7KYawGf1t63hmJwDcU4l09rHA3e0ghmPS8SZxQcbC85cvSKFULYG7p5zoOzGPmrEep2brppMZL71xPX/BDxrB2fnHu1UzUP5yip/jx230PlRs6NFnBN0d73pikRnCTkV8wD5bZi0hzX1U/MiJ30bvxRVn/33n6dAhsQgwKBgQDK57CBxhu8MrNLM0mWA3qgohx6nj8zxAtt879pXDzhu8grzCIzJf37xIR/GsKQB/T02mhCxbPS84HbCop7XQ23IERWrtzO27En1r5/5+4ddKXhJo0HJYKx+LebbXNLQQF4KVu62f9UGPav4OMnPDJ2TfzguGvEXmdyot3Gs6g60wKBgB79nNFj90T1J2MVGlelUD/VDduhBRRA94hO7bIzu1XebaiVVf+7o5d65sORDopka3k3dC+K8MTpON3mfWkbc8H5oP3XVsa1iXWZU4MIol88JYeYOM/xHJgRnmSnkYwpDvNSZLA9w2qATwyD1Fo9m1FPiJ9ks51Ied9in2SpmNS1AoGALcjB50aQk5LNKjbUb+kxDnYF/xUeJcwi2BicJGYPxs/chyV0wh3chab+Tr/3DxD8kROHWynJPdx8fLSQMu34Xk4FnZmvcW3ZN6gZLDtlW3gqxfcw81/fcdExfPzheyWb80VB0ZPHLW7Em0MffobyzAck/IAgsRqsNQ3TBTccyskCgYEAqZk9CQAjmc7HiYBRm3JJesI1ZcLgSHAQeKVOtoTGtVHmD5i0GTB3UQbMkHX+/Ryu/gxCURZmjLuVQmoWpp3zPi0wmR3jqe+ZwuqH88IYv6FUNltuUqTaeao4/+Yr8babqVgL3UAlKRoU9UXsy8NUQRhtMx1wjAIZmUKRvHrSH6E=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjlKyk54ccAHKHSPnaCo57TdrqEors4FqxsmyF4T+7hgrzUR4181bD4hcrSn4lCIGQXCivuV3d9zeFBcCqQjRoUVlUoOGm+TOmtFfzmEd+OVGeqZ6v7FVPHBjtcSfr7pP3OUqu+vIePXXvg7SLcorKNkkOX3i2shabfBfIr2SqPzQIMDTSASqJACMSB35nEWr2k3a/VJAtT4X4zQ0tsPjW0HT0JhVzYFf4Jna+1t5K9/e5kUxwmEtbkC5hh3+/simSqEaPjb69E860xBx6l2SfLe7AvKt7EqHmIOtqF6qV4JhIt9kke1n++YbIp5UL0LOtPmv7ukGS1A12QgCpDTwbwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://0qy9u1mcay.54http.tech/payed/notify";;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url= "http://member.jinggumail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
